//
//  SLStatus.swift
//  Sina15
//
//  Created by 盼 on 16/4/12.
//  Copyright © 2016年 pan. All rights reserved.
//

import UIKit

class SLStatus: NSObject {
    
    //微博id
    var id: Int64 = 0

    //微博创建时间
    var created_at: String?
    
    //微博内容
    var text: String?
    
    //微博来源
    var source: String?
    
    //微博作者的用户信息
    var user: SLUser?
    
    //微博转发数量
    var reposts_count: Int = 0
    
    //评论数量
    var comments_count: Int = 0
    
    //点赞数量
    var attitudes_count: Int = 0
    
    //转发微博
    var retweeted_status: SLStatus?
    
    //配图视图的图片数组
    var pic_urls: [SLStatusPicture]?
    
    //KVC构造函数
    init(dict: [String: AnyObject]) {
        
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    //重写setValue方法
    override func setValue(value: AnyObject?, forKey key: String) {
        
        //微博用户字典转模型
        if key == "user" {
            
            let userDict = value as! [String: AnyObject]
            
            user = SLUser(dict: userDict)
           //不要忘记return,否则字典转模型就不起作用
            return
        }
        
        //转发微博字典转模型
        if key == "retweeted_status" {
            
            let retweetedStatus = value as! [String: AnyObject]
            
            retweeted_status = SLStatus(dict: retweetedStatus)
            
            //不要忘记return，否则字典转模型就不起作用
            return
        }
        
        //微博图片字典转模型
        var pics = [SLStatusPicture]()
        if key == "pic_urls" {
            
            let picArray = value as! [[String: String]]
            //字典转模型
            for item in picArray {
                let pic_url = SLStatusPicture(dict: item)
                
                pics.append(pic_url)
            }
            self.pic_urls = pics
            return
        }
        
        //一定要调用super方法
        super.setValue(value, forKey: key)
    }
    //重写undefined方法
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    //把对象转换成字符串
    override var description: String {
        let keys = ["text"]
        
        let dict = self.dictionaryWithValuesForKeys(keys)
        
        return dict.description
    }
}
